
<template>
	<view class="content">
		<view class="uni-form-item uni-column">
			<input type="number" class="uni-input" name="" v-model="phone" placeholder="请输入手机号" />
		</view>
		<view class="uni-form-item uni-column column-with-btn">
			<input type="text" class="uni-input" v-model="user" placeholder="请输入用户名" autocapitalize="on"/>

		</view>
		<view class="uni-form-item uni-column column-with-btn">
			<input v-model="codes" type="number" class="uni-input" name="" placeholder="请输入验证码" />
			<text disabled @tap="getcode">{{code}}</text>
		</view>
		<view class="uni-form-item uni-column">
			<input type="password" class="uni-input" v-model="pass" name="" placeholder="请输入密码" />
		</view>
		<button type="primary" @tap="regHome">注册</button>
		<view class="links">已有账号？<view class="link-highlight" @tap="gotoLogin">点此登陆</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				captchaImg: '',
				seconds: 10,
				codeBtn: {
					text: '获取验证码',
					waitingCode: false,
					count: this.seconds
				},
				code: '获取验证码',
				isable: true,
				phone: '',
				user: '',
				pass: '',
				codes: ''
			}
		},
		onLoad() {

		},
		methods: {
			sendCode: function() {
				this.codeBtn.waitingCode = true;
				this.codeBtn.count = this.seconds;
				this.codeBtn.text = this.codeBtn.count + 's';

				let countdown = setInterval(() => {
					this.codeBtn.count--;
					this.codeBtn.text = this.codeBtn.count + 's';
					if (this.codeBtn.count < 0) {
						clearInterval(countdown);
						this.codeBtn.text = '重新发送';
						this.codeBtn.waitingCode = false;
					}
				}, 1000);
			},
			
			// 跳转登录
			gotoLogin: function() {
				uni.navigateTo({
					url: '../zhihui/zhihui'
				})
			},
			
			// 注册用户
			regHome() {
				var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
				var myreg_code = /^\d{4}$/
				if(this.phone === ''){
					console.log('手机号不能为空')
				}else if(!myreg.test(this.phone)){
					console.log('手机号格式不正确')
				}else if(this.codes === ''){
					console.log('验证码为空')
				}else if(!myreg_code.test(this.codes)){
					console.log('验证码格式不正确')
				}else if(this.pass === ''){
					console.log('密码不能为空')
				}else if(this.user === ''){
					console.log('用户名不能为空')
				}else{
					var data = {
								username: this.user,
								phone: this.phone,
								password: this.pass,
								smscode: this.codes,
					
					}
					this.http.Loginpost('/sys/userExe/phoneVerification', data)
					.then(res => {
						uni.showToast({
						    title: '注册成功'
						});
						uni.navigateTo({
							url: '/pages/login/login',
						})
					})
					.catch(err => {
						uni.showToast({
						    title: '注册失败！请重新注册'
						});
					})
				}
				
			},
			
			// 获取验证码
			getcode() {
				if (this.isable) {
					var data = {
							phoneNumber: this.phone
					}
					this.http.Loginpost('/sys/userExe/getPhoneVerification', data)
					.then(res => {
					})
					.catch(err => {
					})

					this.isable = false
					this.code = 60
					var a = setInterval(() => {
						this.code = this.code - 1
						if (this.code === 0) {
							clearInterval(a)
							this.code = '获取验证码'
							this.isable = true
						}
					}, 1000)
				}

			},
		},
		computed: {
			disableCodeBtn: function() {
				return this.codeBtn.waitingCode || this.captchaImg.length < 4;
			}
		}
	}
</script>

<style lang="scss" scoped>
	$color-primary: #b49950;

	.content {
		padding: 400upx 100upx 100upx;
	}

	.logo {
		text-align: center;
		height: 250upx;
	}

	.uni-form-item {
		margin-bottom: 40upx;
		padding: 0;
		border-bottom: 1px solid #e3e3e3;

		.uni-input {
			height: 60upx;
			font-size: 30upx;
			padding: 7px 0;
		}
	}

	.column-with-btn {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;

		button {
			font-size: 24upx;
			margin: 0;
			width: 180upx;
			text-align: center;

			&:after {
				border: none
			}

			&.active {
				background-color: $color-primary;
				color: $uni-text-color-inverse;
			}
		}
	}

	.img-captcha {
		width: 150upx;
		height: 60upx;
	}

	button[type="primary"] {
		background-color: $color-primary;
		border-radius: 0;
		font-size: 34upx;
		margin-top: 60upx;
	}

	.links {
		text-align: center;
		margin-top: 40upx;
		font-size: 26upx;
		color: #999;

		view {
			display: inline-block;
			vertical-align: top;
			margin: 0 10upx;
		}

		.link-highlight {
			color: $color-primary
		}
	}
</style>
